﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace 小小次朗点餐系统
{
    class DBHelper
    {
        //静态字段
        public static string connString = "server=.;database=DreamTeam;Integrated Security=true ";
        //数据库链接对象
        public static SqlConnection Conn = null;

        //初始化数据库链接
        public static void InitConnection()
        {
            if (Conn == null)
                Conn = new SqlConnection(connString);
            if (Conn.State == ConnectionState.Closed)
                Conn.Open();
            if (Conn.State == ConnectionState.Broken)
            {
                Conn.Close();
                Conn.Open();
            }
        }
        //静态字段
        public static string connStr = "server=.;database=DreamTeam;Integrated Security=true";
        //静态方法一查询（断开式查询）
        public static DataSet SelectByAdapter(string sql)
        {
            SqlConnection conn = new SqlConnection(connStr);
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }

   
        //静态方法二增删改
        public static bool GetNonQuery(string sql)
        {
            bool result = false;
            //1、创建连接对象
            SqlConnection conn = new SqlConnection(connStr);
            //2、打开连接
            conn.Open();
            //3、创建对象命令
            SqlCommand cmd = new SqlCommand(sql, conn);
            //4、ExecuteNonQuery执行(增删改)
            int number = cmd.ExecuteNonQuery();
            if (number > 0)
            {
                result = true;
            }
            conn.Close();
            return result;
        }
        public static object ExecuteScalar(string sqlStr)
        {
            InitConnection();
            SqlCommand cmd = new SqlCommand(sqlStr, Conn);
            object result = cmd.ExecuteScalar();
            Conn.Close();
            return result;
        }

    }
}

 
